using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace CollegeShop.Infrastructure.Entities.Repository
{
    [Table("T_Issue")]
    public class T_Issue : IEntity<T_Issue>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(32)]
        public string IssueNumber { get; set; }

        public int SumAmount { get; set; }

        public decimal SumSaleMoney { get; set; }

        public int SalesUnitID { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string CreateUser { get; set; }

        public DateTime CreateTime { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(32)]
        public string DeliveryUser { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(32)]
        public string DeliveryPhone { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string ReceivedUser { get; set; }

        public DateTime ReceivedTime { get; set; }

        public int State { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string Remark { get; set; }

        public override T_Issue Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.ID == id);
        }

        public override T_Issue Find(System.Linq.Expressions.Expression<Func<T_Issue, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Issue.FirstOrDefault(where);
                if (item != null)
                {
                    this.ID = item.ID;
                    this.IssueNumber = item.IssueNumber;
                    this.SumAmount = item.SumAmount;
                    this.SumSaleMoney = item.SumSaleMoney;
                    this.SalesUnitID = item.SalesUnitID;
                    this.CreateUser = item.CreateUser;
                    this.CreateTime = item.CreateTime;
                    this.DeliveryUser = item.DeliveryUser;
                    this.DeliveryPhone = item.DeliveryPhone;
                    this.ReceivedUser = item.ReceivedUser;
                    this.ReceivedTime = item.ReceivedTime;
                    this.State = item.State;
                    this.Remark = item.Remark;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Issue, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Issue.Any(where);
            }
        }
    }
}
